home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Human Interface Toolbox / BackScrap / BackScrap.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  5.1 KB  |  196 lines  |  [TEXT/CWIE]

  1. /*
  2.     You may incorporate this Apple sample source code into your program(s) without
  3.     restriction. This Apple sample source code has been provided "AS IS" and the
  4.     responsibility for its operation is yours. You are not permitted to redistribute
  5.     this Apple sample source code as "Apple sample source code" after having made
  6.     changes. If you're going to re-distribute the source, we require that you make
  7.     it clear in the source that the code was descended from Apple sample source
  8.     code, but that you've made changes.
  9.  
  10.     PLEASE PLEASE PLEASE DO send detailed bug reports to <devbugs@apple.com>.
  11. */
  12.  
  13. #ifndef OLDROUTINELOCATIONS
  14. #    define OLDROUTINELOCATIONS 0
  15. #elif OLDROUTINELOCATIONS
  16. #    error OLDROUTINELOCATIONS must be FALSE when compiling MoreIsBetter.
  17. #endif
  18.  
  19. #ifndef OLDROUTINENAMES
  20. #    define OLDROUTINENAMES 0
  21. #elif OLDROUTINENAMES
  22. #    error OLDROUTINENAMES must be FALSE when compiling MoreIsBetter.
  23. #endif
  24.  
  25. #include <Scrap.h>
  26. #include <Processes.h>
  27. #include <Resources.h>
  28. #include <Patches.h>
  29. #include <A4Stuff.h>
  30. #include <Traps.h>
  31.  
  32. ////////////////////////////////////////////////////////////////////////////////////
  33.  
  34. static pascal Boolean HaveProcessManager (void)
  35. {
  36.     return GetToolTrapAddress (_OSDispatch) != GetToolTrapAddress (_Unimplemented);
  37. }
  38.  
  39. #define CaseString(c,s) case (c) : (s) = "\p"#c" called from app in background."; break
  40.  
  41. static pascal void CheckBackground (UInt16 trapWord)
  42. {
  43.     if (HaveProcessManager ( ))
  44.     {
  45.         ProcessSerialNumber        frontPSN;
  46.         ProcessSerialNumber        curPSN;
  47.         Boolean                    sameProcess;
  48.  
  49.         if (!GetFrontProcess (&frontPSN))
  50.         if (!GetCurrentProcess (&curPSN))
  51.         if (!SameProcess (&frontPSN,&curPSN,&sameProcess) && !sameProcess)
  52.         {
  53.             ConstStr255Param trapMessage = nil;
  54.  
  55.             switch (trapWord)
  56.             {
  57.                 CaseString (_PutScrap,        trapMessage);
  58.                 CaseString (_InfoScrap,        trapMessage);
  59.                 CaseString (_UnloadScrap,    trapMessage);
  60.                 CaseString (_LoadScrap,        trapMessage);
  61.                 CaseString (_GetScrap,        trapMessage);
  62.                 CaseString (_ZeroScrap,        trapMessage);
  63.             }
  64.  
  65.             if (trapMessage)
  66.                 DebugStr (trapMessage);
  67.         }
  68.     }
  69. }
  70.  
  71. ////////////////////////////////////////////////////////////////////////////////////
  72.  
  73. typedef pascal SInt32 (*vPutScrap) (    SInt32             length,
  74.                                         ResType         theType,
  75.                                         const void *    source        );
  76.  
  77. static vPutScrap gPutScrap;
  78.  
  79. static pascal SInt32 MyPutScrap (    SInt32             length,
  80.                                     ResType         theType,
  81.                                     const void *    source        )
  82. {
  83.     long preservedA4 = SetCurrentA4 ( );
  84.     SInt32 result = gPutScrap (length,theType,source);
  85.     CheckBackground (_PutScrap);
  86.     SetA4 (preservedA4);
  87.     return result;
  88. }
  89.  
  90. ////////////////////////////////////////////////////////////////////////////////////
  91.  
  92. typedef pascal ScrapStuffPtr (*vInfoScrap) (void);
  93.  
  94. static vInfoScrap gInfoScrap;
  95.  
  96. static pascal ScrapStuffPtr MyInfoScrap (void)
  97. {
  98.     long preservedA4 = SetCurrentA4 ( );
  99.     ScrapStuffPtr result = gInfoScrap ( );
  100.     CheckBackground (_InfoScrap);
  101.     SetA4 (preservedA4);
  102.     return result;
  103. }
  104.  
  105. ////////////////////////////////////////////////////////////////////////////////////
  106.  
  107. typedef pascal SInt32 (*vUnloadScrap) (void);
  108.  
  109. static vUnloadScrap gUnloadScrap;
  110.  
  111. static pascal SInt32 MyUnloadScrap (void)
  112. {
  113.     long preservedA4 = SetCurrentA4 ( );
  114.     SInt32 result = gUnloadScrap ( );
  115.     CheckBackground (_UnloadScrap);
  116.     SetA4 (preservedA4);
  117.     return result;
  118. }
  119.  
  120. ////////////////////////////////////////////////////////////////////////////////////
  121.  
  122. typedef pascal SInt32 (*vLoadScrap) (void);
  123.  
  124. static vLoadScrap gLoadScrap;
  125.  
  126. static pascal SInt32 MyLoadScrap (void)
  127. {
  128.     long preservedA4 = SetCurrentA4 ( );
  129.     SInt32 result = gLoadScrap ( );
  130.     CheckBackground (_LoadScrap);
  131.     SetA4 (preservedA4);
  132.     return result;
  133. }
  134.  
  135. ////////////////////////////////////////////////////////////////////////////////////
  136.  
  137. typedef pascal SInt32 (*vGetScrap) (    Handle             hDest,
  138.                                         ResType         theType,
  139.                                         SInt32            *offset        );
  140.  
  141. static vGetScrap gGetScrap;
  142.  
  143. static pascal SInt32 MyGetScrap (    Handle         hDest,
  144.                                     ResType        theType,
  145.                                     SInt32 *    offset        )
  146. {
  147.     long preservedA4 = SetCurrentA4 ( );
  148.     SInt32 result = gGetScrap (hDest,theType,offset);
  149.     CheckBackground (_GetScrap);
  150.     SetA4 (preservedA4);
  151.     return result;
  152. }
  153.  
  154. ////////////////////////////////////////////////////////////////////////////////////
  155.  
  156. typedef pascal SInt32 (*vZeroScrap) (void);
  157.  
  158. static vZeroScrap gZeroScrap;
  159.  
  160. static pascal SInt32 MyZeroScrap (void)
  161. {
  162.     long preservedA4 = SetCurrentA4 ( );
  163.     SInt32 result = gZeroScrap ( );
  164.     CheckBackground (_ZeroScrap);
  165.     SetA4 (preservedA4);
  166.     return result;
  167. }
  168.  
  169. ////////////////////////////////////////////////////////////////////////////////////
  170.  
  171. static pascal void PatchToolTrap (void *oldAddress, UInt16 trapWord, void *newAddress)
  172. {
  173.     *((void **) oldAddress) = GetToolTrapAddress (trapWord);
  174.     SetToolTrapAddress (UniversalProcPtr (newAddress), trapWord);
  175. }
  176.  
  177. #define PatchScrapTrap(name) \
  178.     PatchToolTrap(&g##name##Scrap,_##name##Scrap,My##name##Scrap)
  179.  
  180. extern "C" { void __Startup__ (void); }
  181.  
  182. void main (void)
  183. {
  184.     long preservedA4 = SetCurrentA4 ( );
  185.  
  186.     PatchScrapTrap (Put);
  187.     PatchScrapTrap (Info);
  188.     PatchScrapTrap (Unload);
  189.     PatchScrapTrap (Load);
  190.     PatchScrapTrap (Get);
  191.     PatchScrapTrap (Zero);
  192.  
  193.     DetachResource (RecoverHandle (Ptr (__Startup__)));
  194.     SetA4 (preservedA4);
  195. }
  196.